<?php

namespace App\Admin\Controllers;

use App\Models\Coupon;
use App\Models\Shop;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Box;


class CouponController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Coupon::class, function (Grid $grid) {
            if(request('shop_id')){
                $grid->model()->where('shop_id',request('shop_id'));
            }
            $grid->disableCreateButton();
            $grid->filter(function($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('shop.shop_name', '店铺名');
                $filter->like('coupon_name', '优惠券名称');
            });
            $grid->id('ID')->sortable();
            $shop_id = request('shop_id');
            $grid->define_preg(url("admin/coupon/create?shop_id=$shop_id"), '生成优惠券');
            $grid->column('shop_id', '店铺名')->display(function ($shop_id){
                $shopname = Shop::where('id',$shop_id)->first();
                $shopname = $shopname['shop_name'];
                return isset($shopname) ? $shopname : '无';
            });
            $grid->column('coupon_name', '优惠券名称');
            $grid->column('coupon_money', '优惠券面额');
            $grid->column('coupon_type', '优惠券类型')->display(function ($coupon_type){
                if($coupon_type == 1){
                    return "<span class='badge bg-red-gradient'>满减</span>";
                }else if($coupon_type == 2){
                    return "<span class='badge bg-blue-gradient'>折扣</span>";
                }else{
                    return '空';
                }
            });
            $grid->column('spend_money', '最低消费金额')->display(function ($spend_money){
                return $spend_money ? $spend_money : '无限制';
            });
            $grid->column('receive_num', '领取数量');
            $grid->column('note', '优惠活动描述');
            $grid->column('coupon_start_time', '优惠开始时间');
            $grid->column('coupon_end_time', '优惠结束时间');

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Coupon::class, function (Form $form) {

            $form->saving(function (Form $form){

                if($form->send_num<=0){
                    throw new \Exception('发放数量必须大于0！');
                }

                Coupon::insert([
                    'shop_id' => $form->shop_id,
                    'coupon_name' => $form->coupon_name,
                    'coupon_type' => $form->coupon_type,
                    'coupon_money' => $form->coupon_money,
                    'receive_num' => $form->receive_num,
                    'note' => $form->note,
                    'coupon_start_time' => $form->coupon_start_time,
                    'coupon_end_time' => $form->coupon_end_time,
                    'created_at' => date('Y-m-d H:i:s',time())
                ]);
                echo "<script> alert('添加优惠券成功！');history.go(-2); </script>";
                die;

            });

            $form->display('id', 'ID');
            if(request('shop_id')) {
                $form->text('shop_id', '店铺id')->value(request('shop_id'))->rules('required');
                $shopname = Shop::find(request('shop_id'))->shop_name;
                $form->display('shop_name', '店铺名')->value($shopname);
            }else{
                if(!request()->route()->parameters()) {
                    $data = Shop::all();
                    $res = array();
                    foreach ($data as $key => $value) {
                        $res[$value['id']] = $value['shop_name'];
                    }
                    $form->select('shop_id', '店铺名')->options($res);
                }
            }
            $form->text('coupon_name', '优惠券名称')->rules('required');
            $form->radio('coupon_type', '优惠券类型')->options(['1' => '满减', '2'=> '折扣'])->default('1');
            $form->currency('coupon_money', '优惠券面额')->symbol('￥')->rules('required');
            $form->currency('spend_money', '最低消费金额')->symbol('￥')->rules('required');
            if(request()->route()->parameters()) {
                $form->text('receive_num', '领取数量');
            }
            $form->textarea('note', '优惠活动描述')->rows('3')->rules('max:255');
            $form->datetimeRange('coupon_start_time','coupon_end_time', '优惠开始和结束时间');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
